package nina.yiyou.performance.s11collect.input

import groovy.transform.PackageScope
import nina.yiyou.salary.input.util.CellValueGetter
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.ss.usermodel.Sheet

import java.util.stream.Collectors

import static java.util.stream.Collectors.toMap

@PackageScope
class OtherEvalExtractor {

  OtherEvalExtractor(Sheet sheet, String fillerId, String fillName) {
    _sheet = sheet
    _fillerId = fillerId
    _fillName = fillName
  }

  Map<String, CollectFilledReader.Eval> extract() {
    Row headerRow = _sheet.getRow(1)
    List<Row> dataRows = (1..18).collect { _sheet.getRow(it) }
    Map groupCol = new GroupColumnGetter(dataRows).getColumn()

    def getColumn = { int c ->
      return dataRows.stream()
          .map { it.getCell(c) }
          .map { new CellValueGetter(it).get() }
          .collect(Collectors.toList())
    }

    def toEvalList = { List valueCol ->
      String[] header = ((String) valueCol[0]).split('_')
      String evaluateeId = (header.length <= 1) ? "[#漏填工号!${header[0]}]" : header[1]
      String evaluateeName = header[0]

      def toEvalItem = { int row ->
        new EvalItemMaker(evaluateeId, evaluateeName,
            _fillerId, _fillName, groupCol, valueCol, row).make()
      }

      return (1..<valueCol.size()).stream()
          .map { toEvalItem(it) }
          .collect(Collectors.toList())
    }

    return (8..<headerRow.lastCellNum).stream()
        .map { getColumn(it) }
        .map { toEvalList(it) }
        .collect(toMap({ List<CollectFilledReader.Eval> e -> e[0].evaluateeId() }, { it }))
  }

  private final Sheet _sheet

  private final String _fillerId
  private final String _fillName
}
